package icu.groot.alan.service.impl;

import icu.groot.alan.dao.DepartmentDao;
import icu.groot.alan.dao.UserDao;
import icu.groot.alan.dao.impl.DepartmentDaoImpl;
import icu.groot.alan.dao.impl.UserDaoImpl;
import icu.groot.alan.entity.Department;
import icu.groot.alan.entity.Paging;
import icu.groot.alan.service.DepartmentService;
import icu.groot.alan.util.Dao;
import icu.groot.alan.util.R;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Logger;

public class DepartmentServiceImpl implements DepartmentService {

    Logger logger = Logger.getLogger("icu.groot.alan.service.impl.DepartmentServiceImpl");
    DepartmentDao departmentDao = new DepartmentDaoImpl();
    UserDao userDao = new UserDaoImpl();
    @Override
    public void paging(Paging<Department> paging) {
        Connection connection = Dao.getConnection();
        int rowCount = departmentDao.getCount(connection, paging.getFuzzyQuery());
        paging.setPageCount((int) Math.ceil((double)rowCount / (double) paging.getPageSize()));
        paging.setRowCount(rowCount);
        departmentDao.paging(paging, connection);
        Dao.closeResource(connection);
    }

    @Override
    public R deleted(String ids){
        Connection connection = Dao.getConnection();
        String[] idsStr = ids.split(",");
        int[] idsInt = new int[idsStr.length];
        int updateRow = 0;
        try {
            for (int i = 0; i < idsStr.length; i++) {
                idsInt[i] = Integer.parseInt(idsStr[i]);
                if (idsInt[i] == 1 || idsInt[i] == 2) return R.error("不能删除管理员或未分配部门");
            }
            updateRow = departmentDao.deleted(connection, idsInt);
            if (updateRow != idsInt.length)return R.error("删除部门数量和传入的数量不相等,请联系管理员纠正数据");
            userDao.updateDepartmentByDepId(connection, idsInt);
            connection.commit();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                e.printStackTrace();
            }
            e.printStackTrace();
            return R.error("参数错误");
        }
        Dao.closeResource(connection);
        return R.ok("已删除" + updateRow + "条数据");
    }

    @Override
    public List<Department> getDepAll() {
        Connection connection = Dao.getConnection();
        List<Department> depAll = departmentDao.getDepAll(connection);
        Dao.closeResource(connection);
        return depAll;

    }

    @Override
    public R add(String name) {
        Connection connection = Dao.getConnection();
        int updateRow = 0;
        try {
            updateRow = departmentDao.add(connection, name);
            connection.commit();
            if(updateRow <= 0) throw new RuntimeException("返回修改行数不大于0");
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                e.printStackTrace();
            }
            e.printStackTrace();
            return R.error("系统异常，请联系管理员");
        }
        Dao.closeResource(connection);
        return R.ok("添加成功");
    }
}
